<?php 
class Admin_UserController extends Zend_Controller_Action
{
	public function indexAction()
	{		
		$this->_forward('list', 'user', 'admin');
	}
	
	public function listAction()
	{
		$users = User::getInstance()->getUserList();
		$this->view->users = $users;		
	}
	
	public function createAction()
	{
		$this->_helper->layout()->disableLayout();
	}
	
	public function submitcreateAction()
	{
		// Disable template
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		$params = $this->_getAllParams();
		$params['active'] = 1;
		$params['created'] = time();
		$validator = new Zend_Validate_EmailAddress();
		if($validator->isValid($params['email']) && strlen($params['password']) && strlen($params['fullname'])) {
			if(!User::getInstance()->checkEmailExist($params['email'])) { 
				if(User::getInstance()->saveUser($params)) {
					$this->_returnState(1);
				}
			}
		}
		$this->_returnState(0);
	}
	
	public function editAction()
	{
		$this->_helper->layout()->disableLayout();
		$id = $this->_getParam('id');
		$user = User::getInstance()->getUser($id);
		$this->view->user = $user;		
	}
	
	public function submiteditAction()
	{
		// Disable template
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		$params = $this->_getAllParams();
		$params['updated'] = time();
		if(strlen($params['fullname'])) {
			if(strlen($params['password']) == 0) {
				$params['password'] = null;
			}
			if(User::getInstance()->saveUser($params)) {
					$this->_returnState(1);
			}
		}
		$this->_returnState(0);	
	}
	
	public function deleteAction()
	{
		// Disable template
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$id = (int)$this->getRequest()->getParam('id');
		if( User::getInstance()->deleteUser($id) ) {
			return $this->_returnState(1);
		} else {
			return $this->_returnState(0);
		}
	}
	
	public function orderAction()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		$params = $this->_getAllParams();		
		foreach($params as $k => $v) {
			$item = str_replace("order_", "", $k);
			if($item > 0 && $v >= 0) {
				$list = array(
					'user_id' => $item,
					'ordering' => $v
				);
				User::getInstance()->saveUser($list);
			}
		}
		$this->_forward('index');
	}
	
	public function approveAction()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		$params = $this->_getAllParams();
		if($params['user_id'] > 0) {
			$list = array(
				'user_id' => $params['user_id'],
				'active' => (($params['state'] == 0)?1:0)
			);
			if(User::getInstance()->saveUser($list)) {
				return $this->_returnState(1);
			}
		}
		return $this->_returnState(0);
	}
	
	public function loginAction()
	{
		$this->_helper->layout()->disableLayout();
		// Check Login
		$isLogin = new Zend_Session_Namespace('admin');
		if($isLogin->login === true) {
			$this->_redirect($this->view->serverUrl().$this->view->url(array('module'=>'admin', 'controller'=>'index', 'action'=>'index'), null, true));
		}	
	}
	
	public function submitloginAction()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		// Check Login
		$isLogin = new Zend_Session_Namespace('admin');
		if($isLogin->login === true) {
			$this->_redirect($this->view->serverUrl().$this->view->url(array('module'=>'admin', 'controller'=>'index', 'action'=>'index'), null, true));
		}
		
		// Process login
		$params = $this->_getAllParams();
		$validator = new Zend_Validate_EmailAddress();
		if(strlen($params['password']) && $validator->isValid($params['email'])) {
			// Check Login
			$user = User::getInstance()->checkLogin($params['email'], $params['password']);
			if($user !== false) {
				// Login ok
				$isLogin->login = true;
				$isLogin->user = $user;
				$this->_redirect($this->view->serverUrl().$this->view->url(array('module'=>'admin', 'controller'=>'index', 'action'=>'index'), null, true));
			}
		}	
		// Login fail
		$this->_redirect($this->view->serverUrl().$this->view->url(array('module'=>'admin', 'controller'=>'user', 'action'=>'login'), null, true));
	}
	
	public function logoutAction()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		$isLogin = new Zend_Session_Namespace('admin');
		$isLogin->unsetAll();
		$this->_redirect($this->view->serverUrl().$this->view->url(array('module'=>'admin', 'controller'=>'user', 'action'=>'login'), null, true));
	}
	
	private function _returnState($state)
	{
        echo json_encode(array(
            'status' => $state
        ));
        exit();
	}
}
?>